import { BasicColumn, FormSchema } from '/@/components/Table';
import { render } from '/@/utils/common/renderUtils';
import moment from 'moment-timezone';
import { ajaxGetAllClientList } from './tokens.api';

export const columns: BasicColumn[] = [
  {
    title: 'token',
    dataIndex: 'tokenValue',
    width: 180,
  },
  {
    title: '到期时间',
    dataIndex: 'expiration',
    width: 180,
    customRender: ({ text, record }) => {
      const dateString = moment(text).format('YYYY-MM-DD HH:mm:ss');
      return dateString;
    },
  },
  {
    title: '用户名',
    dataIndex: 'username',
    sorter: true,
    width: 180,
  },
  {
    title: '授权类型',
    dataIndex: 'grantType',
    width: 180,
  },
  {
    title: '所属应用',
    dataIndex: 'clientId',
    width: 180,
  },
  {
    title: '账号类型',
    dataIndex: 'accountType',
    width: 120,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'tenantId',
    label: '所属应用',
    colProps: { span: 6 },
    component: 'ApiSelect',
    componentProps: ({ formModel, tableAction }) => {
      return {
        api: async (params) => {
          const data = await ajaxGetAllClientList(params);
          if (data && data.length > 0) {
            formModel.tenantId = data[0].clientId;
          }
          tableAction.reload();
          return data;
        },
        labelField: 'clientName',
        valueField: 'clientId',
      };
    },
  },
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: { span: 6 },
  },
];
